﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using Fcdbas.Core.Net;
using FileTransferProtocol;

namespace FileTransferClient
{
    public partial class dlgProxySetting : Form
    {
        private Config config;
        public dlgProxySetting(Config config)
        {
            InitializeComponent();
            this.config = config;
        }
        private Fcdbas.Core.Net.ProxyInfo _ProxyInfo = null;
        public Fcdbas.Core.Net.ProxyInfo ProxyInfo
        {
            get
            {
                return _ProxyInfo;
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            config.IP = txtHost.Text;
            config.Port = int.Parse(txtPort.Text);
            if (rbNoProxy.Checked)
                _ProxyInfo = null;
            if (rbUserProxy.Checked)
            {
                config.ProxyUse = ProxyUse.UseProxy;
                config.UserProxy.Domain = txtProxyDomain.Text;
                config.UserProxy.Password = txtProxyPass.Text;
                config.UserProxy.Port = int.Parse(txtProxyPort.Text);
                config.UserProxy.ProxyType = cboProxyType.Text;
                config.UserProxy.Server = txtProxyHost.Text;
                config.UserProxy.User = txtProxyUser.Text;

                //_ProxyInfo = new ProxyInfo((ProxyType)Enum.Parse(typeof(ProxyType), "pt" + cboProxyType.Text),
                //                new IPEndPoint(IPAddress.Parse(txtProxyHost.Text), Convert.ToInt32(txtProxyPort.Text)),
                //                new NetworkCredential(txtProxyUser.Text, txtProxyPass.Text, txtProxyDomain.Text));
            }
            else if (rbIEProxy.Checked)
            {
                config.ProxyUse = ProxyUse.IE;
                //IEProxyInfo info = IEProxyInfo.GetIEProxyInfo();
                //if (info.Enabled && !string.IsNullOrEmpty(info.Server))
                //{
                //    string[] ips = info.Server.Split(':');
                //    if (ips.Length > 1)
                //    {
                //        _ProxyInfo = new ProxyInfo(ProxyType.ptHTTP, 
                //               new IPEndPoint(IPAddress.Parse(ips[0]), Convert.ToInt32(ips[1])),
                //               null);
                //    }

                //}

            }
            else
                config.ProxyUse = ProxyUse.NoUse;
            _ProxyInfo = Config.CreateProxy(config);
        }

        private void dlgProxySetting_Load(object sender, EventArgs e)
        {
            txtHost.Text = config.IP;
            txtPort.Text = config.Port.ToString();
            if (config.ProxyUse == ProxyUse.NoUse)
                rbNoProxy.Checked = true;
            if (config.ProxyUse == ProxyUse.IE)
                rbIEProxy.Checked = true;
            if (config.ProxyUse == ProxyUse.UseProxy)
            {
                rbUserProxy.Checked = true;
                SetUserProxy();
            }
        }
        private void SetUserProxy()
        {
            cboProxyType.Text = "st" + config.UserProxy.ProxyType;
            txtProxyHost.Text = config.UserProxy.Server;
            txtProxyPort.Text = config.UserProxy.Port.ToString();
            txtProxyUser.Text = config.UserProxy.User ;
            txtProxyPass.Text = config.UserProxy.Password;
            txtProxyDomain.Text = config.UserProxy.Domain;
        }

        private void rbUserProxy_CheckedChanged(object sender, EventArgs e)
        {
            SetUserProxy();
        }
    }
}
